package com.joker.airoleplayplatformback.controller;

import com.joker.airoleplayplatformback.domain.entity.R;
import com.joker.airoleplayplatformback.domain.po.KnowledgeBasePO;
import com.joker.airoleplayplatformback.service.KnowledgeBaseService;
import com.joker.airoleplayplatformback.utils.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 知识库控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/knowledge")
@Tag(name = "知识库管理", description = "知识库相关接口")
public class KnowledgeBaseController {

    @Autowired
    private KnowledgeBaseService knowledgeBaseService;

    @Autowired
    private JwtUtils jwtUtils;

    @PostMapping("/upload-file")
    @Operation(summary = "上传文档到知识库")
    public R<KnowledgeBasePO> uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam("name") String name,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "userId") Long userId) {
        try {
            // 注意：这里为了简化，直接从参数获取userId，实际应该从token中获取
            KnowledgeBasePO knowledgeBasePO = knowledgeBaseService.uploadDocument(userId, name, description, file);
            return R.ok(knowledgeBasePO);
        } catch (Exception e) {
            log.error("文档上传失败", e);
            return R.error(500,"文档上传失败: " + e.getMessage());
        }
    }

    @PostMapping("/upload-text")
    @Operation(summary = "上传文本到知识库")
    public R<KnowledgeBasePO> uploadText(@RequestBody UploadTextRequest request) {
        try {
            // 注意：这里为了简化，直接从参数获取userId，实际应该从token中获取
            KnowledgeBasePO knowledgeBasePO = knowledgeBaseService.uploadText(
                request.getUserId(), 
                request.getName(), 
                request.getDescription(), 
                request.getContent()
            );
            return R.ok(knowledgeBasePO);
        } catch (Exception e) {
            log.error("文本上传失败", e);
            return R.error(500,"文本上传失败: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    @Operation(summary = "获取用户的知识库列表")
    public R<List<KnowledgeBasePO>> getKnowledgeBaseList(
            @RequestParam("userId") Long userId) {
        try {
            // 注意：这里为了简化，直接从参数获取userId，实际应该从token中获取
            List<KnowledgeBasePO> knowledgeBasePOS = knowledgeBaseService.getUserKnowledgeBases(userId);
            return R.ok(knowledgeBasePOS);
        } catch (Exception e) {
            log.error("获取知识库列表失败", e);
            return R.error(500,"获取知识库列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取知识库详情")
    public R<KnowledgeBasePO> getKnowledgeBase(@PathVariable Long id) {
        try {
            KnowledgeBasePO knowledgeBasePO = knowledgeBaseService.getKnowledgeBaseById(id);
            if (knowledgeBasePO == null) {
                return R.error(500,"知识库不存在");
            }
            return R.ok(knowledgeBasePO);
        } catch (Exception e) {
            log.error("获取知识库详情失败", e);
            return R.error(500,"获取知识库详情失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除知识库")
    public R<Boolean> deleteKnowledgeBase(
            @PathVariable Long id,
            @RequestParam("userId") Long userId) {
        try {
            // 注意：这里为了简化，直接从参数获取userId，实际应该从token中获取
            boolean result = knowledgeBaseService.deleteKnowledgeBase(id, userId);
            return R.ok(result);
        } catch (Exception e) {
            log.error("删除知识库失败", e);
            return R.error(500,"删除知识库失败: " + e.getMessage());
        }
    }

    // 请求DTO类
    public static class UploadTextRequest {
        private Long userId;
        private String name;
        private String description;
        private String content;

        // getters and setters
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }
        public String getContent() { return content; }
        public void setContent(String content) { this.content = content; }
    }
}
